<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: AdminFieldsController.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_AdminFieldsController extends Fields_Controller_AdminAbstract {
	
	protected $_fieldType = 'sitepage_page';
	protected $_requireProfileType = true;
	
	//ACTION FOR SHOWING THE PROFILE FIELDS
	public function indexAction() {
		
		//GET NAVIGATION
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_admin_main', array (), 'sitepage_admin_main_fields' );
		
		include APPLICATION_PATH . '/application/modules/Sitepage/controllers/license/license2.php';
	}
	
	//ACTION FOR PROFILE FIELD CREATION
	public function fieldCreateAction() {
		parent::fieldCreateAction ();
		
		//GENERATE FORM
		$form = $this->view->form;
		
		if ($form) {
			
			//$form->setTitle('Add Profile Question');
			$form->removeElement ( 'show' );
			$form->addElement ( 'hidden', 'show', array ('value' => 0 ) );
			
			$display = $form->getElement ( 'display' );
			$display->setLabel ( 'Show on profile page?' );
			
			$display->setOptions ( array ('multiOptions' => array (1 => 'Show on profile page', 0 => 'Hide on profile page' ) ) );
			
			$search = $form->getElement ( 'search' );
			$search->setLabel ( 'Show on the search options?' );
			
			$search->setOptions ( array ('multiOptions' => array (0 => 'Hide on the search options', 1 => 'Show on the search options' ) ) );
		}
	}
	
	//ACTION FOR PROFILE FIELD EDITION
	public function fieldEditAction() {
		parent::fieldEditAction ();
		
		//GENERATE FORM
		$form = $this->view->form;
		
		if ($form) {
			
			$form->setTitle ( 'Edit Profile Question' );
			$form->removeElement ( 'show' );
			$form->addElement ( 'hidden', 'show', array ('value' => 0 ) );
			$display = $form->getElement ( 'display' );
			$display->setLabel ( 'Show on profile page?' );
			
			$display->setOptions ( array ('multiOptions' => array (1 => 'Show on profile page', 0 => 'Hide on profile page' ) ) );
			
			$search = $form->getElement ( 'search' );
			$search->setLabel ( 'Show on the search options?' );
			
			$search->setOptions ( array ('multiOptions' => array (0 => 'Hide on the search options', 1 => 'Show on the search options' ) ) );
		}
	}
	
	//ACTION FOR HEADING CREATION
	public function headingCreateAction() {
		parent::headingCreateAction ();
		
		//GENERATE FORM
		$form = $this->view->form;
		
		if ($form) {
			$form->removeElement ( 'show' );
			$form->addElement ( 'hidden', 'show', array ('value' => 0 ) );
			
			$form->removeElement ( 'display' );
			$form->addElement ( 'hidden', 'display', array ('value' => 1 ) );
		}
	}
	
	//ACTION FOR HEADING EDITION
	public function headingEditAction() {
		parent::headingEditAction ();
		
		//GENERATE FORM
		$form = $this->view->form;
		
		if ($form) {
			$form->removeElement ( 'show' );
			$form->addElement ( 'hidden', 'show', array ('value' => 0 ) );
			
			$form->removeElement ( 'display' );
			$form->addElement ( 'hidden', 'display', array ('value' => 1 ) );
		}
	}
	
	//ACTION FOR PROFILE DELETION
	public function typeDeleteAction() {
		$option_id = $this->_getParam ( 'option_id' );
		
		if (! empty ( $option_id )) {
			
			//DELETE FIELD ENTRIES IF EXISTS
			$fieldmapsTable = Engine_Api::_ ()->fields ()->getTable ( 'sitepage_page', 'maps' );
			$select = $fieldmapsTable->select ()->where ( 'option_id =?', $option_id );
			$metaData = $fieldmapsTable->fetchAll ( $select )->toArray ();
			if (! empty ( $metaData )) {
				foreach ( $metaData as $key => $child_ids ) {
					$child_id = $child_ids ['child_id'];
					
					//DELETE FIELD ENTRIES IF EXISTS
					$fieldmetaTable = Engine_Api::_ ()->fields ()->getTable ( 'sitepage_page', 'meta' );
					$fieldmetaTable->delete ( array ('field_id = ?' => $child_id ) );
				}
			}
			
			$fieldmapsTable = Engine_Api::_ ()->fields ()->getTable ( 'sitepage_page', 'maps' );
			$fieldmapsTable->delete ( array ('option_id = ?' => $option_id ) );
			
			$sitepagetable = Engine_Api::_ ()->getDbtable ( 'pages', 'sitepage' );
			$select = $sitepagetable->select ()->from ( $sitepagetable->info ( 'name' ), array ('page_id' ) )->where ( 'profile_type = ?', $option_id );
			$rows = $sitepagetable->fetchAll ( $select )->toArray ();
			if (! empty ( $rows )) {
				foreach ( $rows as $key => $sitepage_ids ) {
					$sitepage_id = $sitepage_ids ['page_id'];
					
					$sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $sitepage_id );
					$sitepage->profile_type = 0;
					$sitepage->save ();
					
					//DELETE FIELD ENTRIES IF EXISTS
					$fieldvalueTable = Engine_Api::_ ()->fields ()->getTable ( 'sitepage_page', 'values' );
					$fieldvalueTable->delete ( array ('item_id = ?' => $sitepage_id ) );
					
					$fieldsearchTable = Engine_Api::_ ()->fields ()->getTable ( 'sitepage_page', 'search' );
					$fieldsearchTable->delete ( array ('item_id = ?' => $sitepage_id ) );
				}
			}
			
			//DELETE MAPPING
			Engine_Api::_ ()->getDbtable ( 'profilemaps', 'sitepage' )->delete ( array ('profile_type = ?' => $option_id ) );
		}
		parent::typeDeleteAction ();
	}
}
?>